<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 14-5-6
 * Time: 下午4:41
 */
class Mgirl extends CI_Model{
    function __construct(){
        parent::__construct();
    }

    function getGirlAll($title,$status=1,$pageSize,$order='id desc'){
        $where = '`status`='.$status;
        if(!empty($title)){
            $where .= ' and `title` like "%'.$title.'%" ';
        }

        $sqlCount = $this->db->query("select * from `tbpopular_girl` where $where");
        $count = $sqlCount->num_rows();

        $sql = "select * from `tbpopular_girl` where {$where} order by $order limit ";

        $query = $this->db->pager($count,$pageSize,$sql);

        //人气女郎
        $girl=array();
        foreach($query['data'] as $value){
            $girl[] = $value['id'];
        }

        //人气女郎对应的用户
        if(!empty($girl)){
            $girlId = implode(',',$girl);

            $sqlGirl = "select * from `tbgirl_user` where `girl_id` in($girlId) ";
            $queryGirl = $this->db->query($sqlGirl);
            $girlArr = array();
            foreach($queryGirl->result() as $value){
                $girlArr[$value->girl_id][] = $value->uid;
            }

        }

        //合并人气女郎和推送用户uid
        foreach($query['data'] as &$value){
            $girlUid = isset($girlArr[$value['id']]) ? implode(',',$girlArr[$value['id']]) :0;
            if(!empty($girlUid)){
                $value['uid'] = $girlUid;
            }else{
                $value['uid'] = '';
            }
        }

        return $query;
    }

    function getGirlById($id)
    {
        $sql = "select * from `tbpopular_girl` where `id`=?";
        $query = $this->db->query($sql,array($id));
        $row = $query->row_array();

        if(!empty($row)){

            $sqlGirl = "select * from `tbgirl_user` where `girl_id`=".$row['id'];
            $queryGirl = $this->db->query($sqlGirl);

            $item = array();
            foreach($queryGirl->result() as $value){
                $item[] = $value->uid;
            }
            $row['uid'] = implode(',',$item);
        }

        return $row;
    }

    function getArticleByUid($uid,$p,$pageSize,$field,$order)
    {
        $sqlCount = "select count(*) as count from `tbarticle` as ta left join `tbarticle_user` as tau on ta.id=tau.article_id where tau.`uid` = ?";
        $queryCount = $this->db->query($sqlCount,array($uid));

        $pagestart = ($p-1)*$pageSize;

        $sqlArticle = "select $field from `tbarticle` as ta left join `tbarticle_user` as tau on ta.id=tau.article_id where tau.`uid` = ? order by $order desc limit $pagestart,$pageSize";
        $articleQuery = $this->db->query($sqlArticle,array($uid));

        $data['data'] = $articleQuery->result();
        $data['page'] = $p;
        $data['pagetotle'] = ceil($queryCount/$pageSize);
        $data['count'] = $queryCount['count'];
        return $data;
    }


    function deleteGirl($ids)
    {
        $sql = "DELETE FROM `tbpopular_girl` where `id` in ($ids)";

        $this->db->query($sql);
    }

    function addGirl($title,$city,$name,$uid,$voteCount,$status,$is_hot)
    {
        //$dataTime = time();
        $sql = "INSERT INTO `tbpopular_girl`(`title`,`city`,`name`,`voteCount`,`status`,`is_hot`)VALUES(?,?,?,?,?,?)";

        $this->db->query($sql,array("$title","$city","$name",$voteCount,$status,$is_hot));
        $lastId = $this->db->insert_id();

        if($lastId && !empty($uid)){
            $this->Mgirluser->inserGirlUser($uid,$lastId);
        }

        return $lastId;
    }

    function editGirl($id,$title,$city,$name,$uid,$voteCount,$is_hot,$status)
    {
        if(!empty($uid)){
            $this->Mgirluser->inserGirlUser($id,$uid);
        }
        $sql = "UPDATE `tbpopular_girl` set `title` = ?,`city` = ?,`name` = ?,`voteCount` = ?,`status` = ?,`is_hot`=? WHERE `id` = ?";
        $this->db->query($sql,array("$title","$city",$name,$voteCount,$status,$is_hot,$id));
        return $this->db->affected_rows();

    }

}